////////////////////////////////////////////////
// File: AdvertisementController.java
// Description: Enter summary here after generation.
// Date: 2016-09-23 09:09:33
// Web: http://www.elanzhou.com/
////////////////////////////////////////////////
package com.mvc.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

//import com.mvc.entity.User;
//import com.mvc.entity.UserDetail;
import com.mvc.entity.Advertisement;
import com.mvc.entity.Meminfo;
import com.mvc.service.AdvertisementService;
import com.mvc.service.DictService;
import com.pub.MyJson;
import com.pub.Page;
import com.aspose.cells.Cells;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
@Controller
@RequestMapping("Advertisement")
public class AdvertisementController {
	protected final transient Log logger = LogFactory.getLog(AdvertisementController.class);
	@Autowired
	private AdvertisementService advertisementService;
	@Autowired
	private DictService dictService;
	public AdvertisementController() {
	}

	/*列表*/
	@RequestMapping("Index.do")
	public String Index(HttpSession session,ModelMap modelMap, String pageNo, String Ajax) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		Page page = new Page();
		page.setPageNox(pageNo);
		page.setPageSize(15);
		String hql = "select a from Advertisement a order by a.ID desc";
		List list = advertisementService.getAdvertisementList(hql, page);
		String counthql = "select count(*) from Advertisement a";
		page.setPageCount(advertisementService.getCount(counthql, page));
		modelMap.put("page", page);
		modelMap.put("list", list);
		if (Ajax != null && Ajax.equals("Ajax")) {
			return "/Advertisement/List";
		} else {
			return "/Advertisement/Index";
		}

	}
	/*添加*/ 
	@RequestMapping("Add.do")
	public String Add(HttpSession session,ModelMap modelMap)
	{
		List adlist=dictService.getAllByKeyname("广告类型");		
		modelMap.put("adlist",adlist);
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		String dqsj=format.format(date);
		modelMap.put("dqsj", dqsj);
		return "/Advertisement/Add";
	}
	/*保存*/
	@RequestMapping("Save.do")
	public void Save(HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, HttpSession session) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);

		try {
			
			Advertisement  advertisement= new Advertisement();
			boolean tag=true;
		    String Msg="信息保存成功";
             
               if(tag){
                              
               String FieldTitle=(String)request.getParameter("Title");
             
               if(((FieldTitle!=null)&(!FieldTitle.trim().equals("")))){
            	   FieldTitle=FieldTitle.trim();
                    advertisement.setTitle(FieldTitle);
                  
        		}else
        		{
        			tag=false;
        			Msg="标题不能为空！";
        		}
        		}
               if(tag){
                   
               String FieldAdtype=(String)request.getParameter("Adtype");
             
               if(((FieldAdtype!=null)&(!FieldAdtype.trim().equals("")))){
            	   FieldAdtype=FieldAdtype.trim();
                    advertisement.setAdtype(FieldAdtype);
                  
        		}else
        		{
        			tag=false;
        			Msg="标题不能为空！";
        		}
        		}
	         	         
             
               if(tag){
                              
               String FieldAddate=(String)request.getParameter("Addate");
             
               if(((FieldAddate!=null)&(!FieldAddate.trim().equals("")))){
            	   FieldAddate=FieldAddate.trim();
                    advertisement.setAddate(FieldAddate);
                  
        		}else
        		{
        			tag=false;
        			Msg="发布时间不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldAdpic=(String)request.getParameter("Adpic");
             
               if(((FieldAdpic!=null)&(!FieldAdpic.trim().equals("")))){
            	   FieldAdpic=FieldAdpic.trim();
                    advertisement.setAdpic(FieldAdpic);
                  
        		}else
        		{
        			tag=false;
        			Msg="图片不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldContxt=(String)request.getParameter("Contxt");
             
               if(((FieldContxt!=null)&(!FieldContxt.trim().equals("")))){
            	   FieldContxt=FieldContxt.trim();
                    advertisement.setContxt(FieldContxt);
                  
        		}else
        		{
        			tag=false;
        			Msg="内容不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldAdrec=(String)request.getParameter("Adrec");
             
               if(((FieldAdrec!=null)&(!FieldAdrec.trim().equals("")))){
            	   FieldAdrec=FieldAdrec.trim();
                    advertisement.setAdrec(FieldAdrec);
                  
        		}else
        		{
        			tag=false;
        			Msg="推荐不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldMemname=(String)request.getParameter("Memname");
             
               if(((FieldMemname!=null)&(!FieldMemname.trim().equals("")))){
            	   FieldMemname=FieldMemname.trim();
                    advertisement.setMemname(FieldMemname);
                  
        		}else
        		{
        			tag=false;
        			Msg="发布人不能为空！";
        		}
        		}
	         
            
            MyJson mj=new MyJson();
              if(tag){
                     advertisementService.Save(advertisement);	 
        			 mj.setStatusCode(200);
        		 }else
        		 {
        			 mj.setStatusCode(300);
        		 }
             mj.setMessage(Msg);
             mj.setForwardUrl("/Advertisement/Index.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
			 response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	} 
    
	/*编辑*/
	@RequestMapping("Edit.do")
	public String Edit(HttpSession session,HttpServletRequest request,ModelMap modelMap)
	{
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		List adlist=dictService.getAllByKeyname("广告类型");		
		modelMap.put("adlist",adlist);
		String ID=request.getParameter("ID");
		Advertisement advertisement=(Advertisement)advertisementService.getAdvertisement(ID);
		modelMap.put("advertisement", advertisement);
		return "/Advertisement/Edit";
	}
    
    /*编辑保存*/
	@RequestMapping("EditSave.do")
	public void EditSave(HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap,HttpSession session) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		try {
			String Id=(String)request.getParameter("ID");
			Advertisement  advertisement=(Advertisement)advertisementService.getAdvertisement(Id);
			boolean tag=true;
		    String Msg="信息修改成功";
             
            if(tag){
               
               String FieldTitle=(String)request.getParameter("Title");
             
               if(((FieldTitle!=null)&(!FieldTitle.trim().equals("")))){
            	   FieldTitle=FieldTitle.trim();
                    advertisement.setTitle(FieldTitle);
                  
        		}else
        		{
        			tag=false;
        			Msg="标题不能为空！";
        		}
        		}
	         
            if(tag){
                
            String FieldAdtype=(String)request.getParameter("Adtype");
          
            if(((FieldAdtype!=null)&(!FieldAdtype.trim().equals("")))){
         	   FieldAdtype=FieldAdtype.trim();
                 advertisement.setAdtype(FieldAdtype);
               
     		}else
     		{
     			tag=false;
     			Msg="标题不能为空！";
     		}
     		}
	         	         
            if(tag){
               
               String FieldAddate=(String)request.getParameter("Addate");
             
               if(((FieldAddate!=null)&(!FieldAddate.trim().equals("")))){
            	   FieldAddate=FieldAddate.trim();
                    advertisement.setAddate(FieldAddate);
                  
        		}else
        		{
        			tag=false;
        			Msg="发布时间不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldAdpic=(String)request.getParameter("Adpic");
             
               if(((FieldAdpic!=null)&(!FieldAdpic.trim().equals("")))){
            	   FieldAdpic=FieldAdpic.trim();
                    advertisement.setAdpic(FieldAdpic);
                  
        		}else
        		{
        			tag=false;
        			Msg="图片不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldContxt=(String)request.getParameter("Contxt");
             
               if(((FieldContxt!=null)&(!FieldContxt.trim().equals("")))){
            	   FieldContxt=FieldContxt.trim();
                    advertisement.setContxt(FieldContxt);
                  
        		}else
        		{
        			tag=false;
        			Msg="内容不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldAdrec=(String)request.getParameter("Adrec");
             
               if(((FieldAdrec!=null)&(!FieldAdrec.trim().equals("")))){
            	   FieldAdrec=FieldAdrec.trim();
                    advertisement.setAdrec(FieldAdrec);
                  
        		}else
        		{
        			tag=false;
        			Msg="推荐不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldMemname=(String)request.getParameter("Memname");
             
               if(((FieldMemname!=null)&(!FieldMemname.trim().equals("")))){
            	   FieldMemname=FieldMemname.trim();
                    advertisement.setMemname(smeminfo.getMemname());
                  
        		}else
        		{
        			tag=false;
        			Msg="发布人不能为空！";
        		}
        		}
	         
            
            MyJson mj=new MyJson();
              if(tag){
                     advertisementService.EditSave(advertisement);	 
        			 mj.setStatusCode(200);
        		 }else
        		 {
        			 mj.setStatusCode(300);
        		 }
             mj.setMessage(Msg);
             mj.setForwardUrl("/Advertisement/Index.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
			 response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	} 
    
	/*删除*/
	@RequestMapping("Del.do")
	public void Del(HttpSession session,HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {
			Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
			modelMap.put("smeminfo",smeminfo);
             String ID=request.getParameter("ID");
             advertisementService.Del(ID);
             String Msg="数据删除成功";
             MyJson mj=new MyJson();
             mj.setStatusCode(200);
             mj.setMessage(Msg);
             mj.setForwardUrl("/Advertisement/Index.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
             try {
                  response.getWriter().print(str);
                 } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 }
	      }
          	/*编辑*/
	@RequestMapping("Detail.do")
	public String Detail(HttpSession session,HttpServletRequest request,ModelMap modelMap)
	{
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		String ID=request.getParameter("ID");
		Advertisement advertisement=(Advertisement)advertisementService.getAdvertisement(ID);
		modelMap.put("advertisement", advertisement);
		return "/Advertisement/Detail";
	}
    
	/* 上传img、file表单 */
	@RequestMapping("Upload.do")
	public void Upload(HttpSession session,ModelMap modelMap,HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(value = "file", required = false) MultipartFile file)
			
			throws IOException {
		
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		byte[] bytes = file.getBytes();

		String uploadDir = request.getRealPath("/") + "upfile";

		System.out.println(uploadDir);
		File dirPath = new File(uploadDir);
		if (!dirPath.exists()) {
			dirPath.mkdirs();
		}

		// java.util.Date dt = new java.util.Date(System.currentTimeMillis());
		// SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		// String fileName= fmt.format(dt);
		String fileName = java.util.UUID.randomUUID().toString();
		File f = new File(file.getOriginalFilename());
		String extName = f.getName();
		String prefix = extName.substring(extName.lastIndexOf(".") + 1);
		fileName = fileName + "." + prefix;
		String sep = System.getProperty("file.separator");
		File uploadedFile = new File(uploadDir + sep + fileName);

		FileCopyUtils.copy(bytes, uploadedFile);
		bytes = null;
		uploadedFile = null;

		String Filenames = "/upfile/" + fileName;
		String msg = "{\"status\":\"200\",\"FileName\":\"" + Filenames + "\"}";
		response.getWriter().write(msg);
	}
 
}
